/**
 * 自定义函数
 * */

var adminLogin = 'adminInfo'; //登录信息
var pageSize = 10;
var apiMethod = 'post';
var apiDomain = '/api/admin/';


(function () {
    if (window.location.pathname != '/ad369/login.html') {
        //getAid();
        getHeader();
        leftMenu();
    }
})();


//显示字符串前几个字符
function strPrefix(str, n){
    if (str.length <= n) {
        return str;
    } else {
        return str.substring(0, n) + '...';
    }
}


//生成随机数
function randInt(min, max) {
    // 加1是因为我们想要包含max值在内
    return Math.floor(Math.random() * (max - min + 1)) + min;
}


// 分离header头部
function getHeader(){
    var h = '';
    h += '<div class="navbar navbar-fixed-top">';
    h += '<div class="container-fluid cl" style="background: #222;">';
    h += '<a class="logo navbar-logo" href="javascript:;" style="color: #fff;">系统后台</a>';
    h += '<nav id="Hui-userbar" class="nav navbar-nav navbar-userbar hidden-xs">';
    h += '<ul class="cl">';
    h += '<li class="dropDown dropDown_hover">';
    h += '<a href="javascript:;" class="dropDown_A" style="color: #fff;">';
    h += 'admin';
    h += ' <i class="Hui-iconfont">&#xe6d5;</i>';
    h += '</a>';
    h += '<ul class="dropDown-menu menu radius box-shadow">';
    h += '<li><a href="javascript:;" onclick="logout();">退出</a></li>';
    h += '</ul>';
    h += '</li>';
    h += '</ul>';
    h += '</nav>';
    h += '</div>';
    h += '</div>';
    $('.navbar-wrapper').html(h);
}


//退出
function logout(){
    delCache({key: adminLogin});
    window.location.href = '/admin/login.html';
}


// 分离左侧menu
function leftMenu(){
    var ms = [
        {
            id: 1,
            title: '首页统计',
            link: 'index.html',
            icon: '&#xe61a;',
            childs: []
        },
        {
            id: 2,
            title: '系统管理',
            link: '',
            icon: '&#xe60d;',
            childs: [
                { id: 201, title: '游客列表', link: 'uvisit.html' },
                { id: 202, title: '微信用户', link: 'uwx.html' },
                { id: 203, title: '小程序', link: 'xcx.html' },
                { id: 204, title: '分类管理', link: 'cate.html' },
                { id: 205, title: '轮播图', link: 'banner.html' },
                { id: 206, title: '二维码', link: 'qr.html' },
                { id: 207, title: '扫码记录', link: 'qrlog.html' },
                { id: 208, title: '社区管理', link: 'shequ.html' },
                { id: 209, title: '收货地址', link: 'address.html' },
            ]
        },
        {
            id: 3,
            title: '业务个人',
            link: '',
            icon: '&#xe616;',
            childs: [
                { id: 301, title: '物品管理', link: 'yw-user/tmp.html' },
            ]
        },
        {
            id: 4,
            title: '业务商业',
            link: '',
            icon: '&#xe616;',
            childs: [
                { id: 401, title: '店面管理', link: 'yw-mall/shop.html' },
                { id: 402, title: '门店商品', link: 'yw-mall/goods.html' },
                { id: 501, title: '家政服务', link: 'yw-mall/fuwu.html' },
            ]
        },
        {
            id: 6,
            title: '推广',
            link: '',
            icon: '&#xe616;',
            childs: [
                { id: 601, title: '广告管理', link: 'yw-tui/ad.html' },
                { id: 602, title: '广告曝光', link: 'yw-tui/adshow.html' },
            ]
        },
    ];
    leftMenuHtml(ms);
}
//左侧菜单字符串
function leftMenuHtml(ms){
    var preUrl = window.location.pathname; //当前相对路径
    var h = '', i, link,link2;
    h += '<style type="text/css">.mlyjt { ;margin-right: 10px; float: right; } .mla { background: #ffffff; }</style>';
    h += '<div>';
    for (i = 0; i < ms.length; i ++) {
        childs = ms[i].childs;
        link = '/ad369/' + ms[i].link;
        h += '<dl >';
        if (ms[i].childs.length > 0) {
            h += '<dt>';
            h += '<i class="Hui-iconfont">' + ms[i].icon + '</i>';
            h += ' &nbsp;' + ms[i].title;
            h += '<i class="Hui-iconfont menu_dropdown-arrow">&#xe6d5;</i>';
            h += '</dt>';
            h += '</dt>';
        } else {
            if (preUrl == link) {
                h += '<a href="' + link + '" class="mla">';
            } else {
                h += '<a href="' + link + '">';
            }
            h += '<dt>';
            h += '<i class="Hui-iconfont">' + ms[i].icon + '</i>';
            h += ' &nbsp;' + ms[i].title;
            if (preUrl == link) {
                h += '<span class="mlyjt">→</span>';
            }
            h += '</dt>';
            h += '</a>';
        }
        h += '<dd style="display: block;">';
        if (ms[i].childs.length > 0) {
            h += '<ul>';
            var childs = ms[i].childs;
            for (var ii = 0; ii < childs.length; ii ++) {
                link2 = '/ad369/' + childs[ii].link;
                //console.log(link2, preUrl);
                h += '<li>';
                if (preUrl == link2) {
                    h += '<a href="' + link2 + '" class="mla">';
                } else {
                    h += '<a href="' + link2 + '">';
                }
                h += childs[ii].title;
                //console.log(preUrl, link2);
                if (preUrl == link2) {
                    h += '<span class="mlyjt">→</span>';
                }
                h += '</a>';
                h += '</li>';
            }
            h += '</ul>';
        }
        h += '</dd>';
        h += '</dt>';
        h += '';
        h += '</dl>';
    }
    h += '<div style="height: 50px;"></div>';
    h += '</div>';
    $('#leftMenu').html(h);
}


//分页字符串
function pageHtml(e){
    console.log(e);
    var start = e.start, limit = e.limit, total = e.total, url = e.url;
    var pageTotal = Math.ceil(total / limit);
    var h = '';
    h += '<style type="text/css"> #pageHtml {margin: 10px 0; width: 100%; text-align: center; color: #555; } #pageBtn {  color:#555; } #pageBtn a { padding: 5px 10px; color: #0088cc; } </style>';
    h += '显示 <span id="pageCurr">' + start + '</span> / <span id="pageTotal">' + pageTotal + '</span> 页，';
    h += '<span id="pageBtn">';
    h += '<a href="javascript:;" onclick="pageHome();">首页</a>';
    h += '<a href="javascript:;" onclick="pagePre();">上一页</a>';
    //h += '<a href="javascript:;" onclick="msgPop({msg: \'当前第' + start + '页\'});">' + start + '</a>';
    h += '<a href="javascript:;" onclick="pageNext();">下一页</a>';
    h += '<a href="javascript:;" onclick="pageEnd();">尾页</a>';
    h += '</span>';
    $('#pageHtml').html(h);
}
//首页
function pageHome(){
    window.location.href = pages.url;
}
//上一页
function pagePre(){
    var start = pages.start * 1 - 1;
    if (start <= 0) {
        msgPop({msg: '已经是第一页了'});
        return
    }
    window.location.href = pages.url + '?start=' + start;
}
//下一页
function pageNext(){
    var start = pages.start * 1 + 1;
    var pageTotal = Math.ceil(pages.total / pages.limit);
    if (start >= pageTotal) {
        msgPop({msg: '已经是最后一页了'});
        return
    }
    window.location.href = pages.url + '?start=' + start;
}
//尾页
function pageEnd(){
    var pageTotal = Math.ceil(pages.total / pages.limit);
    window.location.href = pages.url + '?start=' + pageTotal;
}


//抓图
function uploadImage() {
    var fileObj = $('#bigUpload')[0],
        reader = new FileReader();
    reader.readAsDataURL(fileObj.files[0]); //这一步是关键
    reader.onload = function (res){
        //console.log(res.target.result);
        showImg({img: res.target.result});
    }
}


//登录aid
function getAid(){
    var adminInfo = getAdminInfo();
    if (!adminInfo) {
        msgPop({msg: '没有登录'});
        setTimeout(function () {
            location.href = '/ad369/login.html';
        }, 1000);
        return
    }
    return adminInfo.aid;
}


//自定义弹窗
function msgPop(e){
    var cw = document.documentElement.clientWidth;
    var ch = document.documentElement.clientHeight;
    var msg = e.msg;
    var h = '';
    h += '<div style="position: fixed; top: 0; left: 0; z-index: 10000;">';
    h += '<div style="width: ' + (cw) + 'px; height: ' + (ch) + 'px; background: #000; opacity: 0.2; position: fixed; top: 0; left: 0; z-index: 10000;"></div>';
    h += '<div style="padding: 10px 20px; width: ' + (cw * 0.5) + 'px; text-align: center; background: #000; color: #FFF; border-radius: 5px; position: fixed; bottom: 40%; left: ' + ((cw * 0.5 - 40) / 2) + 'px; z-index: 10000;">'+msg+'</div>';
    h += '</div>';
    h += '<script>setTimeout(function(){$(\'#msgPop\').html(\'\');}, 2000)</script>';
    $('#msgPop').html(h);
}


//layer弹窗
function layerMsgErr(e){
    layer.msg(e,{icon: 0,time: 1000});
    return
}


//layer弹窗
function layerMsgOk(e){
    layer.msg(e,{icon: 1,time: 1000});
    return
}


//获取登录信息
function getAdminInfo(){
    return getCache(adminLogin);
}


//设置登录信息
function setAdminInfo(e){
    return setCache({key: adminLogin, val: e.val});
}


//获取地址栏中参数
function getParamByUrl(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r==null) return '';
    return  unescape(r[2]);
}


//设置缓存
function setCache(e){
    var key = e.key, val = JSON.stringify(e.val);
    window.localStorage.setItem(key, val);
}


//获取缓存
function getCache(key){
    var cache = window.localStorage.getItem(key);
    if (!cache) { return ''; }
    return JSON.parse(cache);
}


//删除指定缓存
function delCache(e){
    var key = e.key;
    window.localStorage.removeItem(key);
}


//清理缓存
function clearCache(){
    window.localStorage.clear();
}